package com.ugrow.ugrowcommunity.activity.seatReservation;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.github.gzuliyujiang.dialog.DialogConfig;
import com.github.gzuliyujiang.dialog.DialogStyle;
import com.github.gzuliyujiang.wheelpicker.OptionPicker;
import com.github.gzuliyujiang.wheelpicker.contract.OnOptionPickedListener;
import com.github.gzuliyujiang.wheelpicker.contract.OnOptionSelectedListener;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.ugrow.ugrowcommunity.BuildConfig;
import com.ugrow.ugrowcommunity.R;
import com.ugrow.ugrowcommunity.activity.seatReservation.adapter.SeatRAdapter;
import com.ugrow.ugrowcommunity.activity.seatReservation.bean.ClassroomBean;
import com.ugrow.ugrowcommunity.data.TimeDate;
import com.ugrow.ugrowcommunity.util.HttpUtil;
import com.ugrow.ugrowcommunity.util.ToastUtil;


import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;


//座位预约页面
public class SeatReservationActivity extends AppCompatActivity implements OnOptionPickedListener {

    private RadioGroup mRadioGroup;
    private ImageView mImgBack;
    private RadioButton mBtNowaDays, mBtTomorrow;
    private RecyclerView recyclerView;
    private SeatRAdapter seatRAdapter;
    private Button mTimeSpinner;
    private SwipeRefreshLayout swipeRefreshLayout;
    private String day;
    private String time;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_seat_reservation);

        initView();
        initAdapter();
        initListOne();
        refresh();

    }

    //初始化页面视图
    private void initView() {
        mRadioGroup = findViewById(R.id.grop);
        mImgBack = findViewById(R.id.ACback);
        recyclerView = findViewById(R.id.rec_seatr);
        mBtNowaDays = findViewById(R.id.bt_nowadays);
        mBtTomorrow = findViewById(R.id.bt_tomorrow);
        mTimeSpinner = findViewById(R.id.nice_spinner);
        swipeRefreshLayout = findViewById(R.id.swipe_refresh_layout);

        mBtNowaDays.setText(TimeDate.getNowaDays() + "\n今天");
        mBtTomorrow.setText(TimeDate.getTommrow() + "\n" + TimeDate.getWeek());
        mTimeSpinner.setText("开始时间\n" + TimeDate.getTime());

        //初始化时间信息
        day = TimeDate.getNowaDays();
        time = mTimeSpinner.getText().toString().substring(5);
        //日期选择
        cheakRadio();

        //返回
        mImgBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


        mTimeSpinner.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onOptionText();
            }
        });


    }


    //日期选择
    private void cheakRadio() {
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.bt_nowadays:
                        day = TimeDate.getNowaDays();
                        break;

                    case R.id.bt_tomorrow:
                        day = TimeDate.getTommrow();
                        break;
                }
                initListOne();
            }
        });
    }

    private void initAdapter() {

        LinearLayoutManager layoutManager = new LinearLayoutManager(SeatReservationActivity.this);
        recyclerView.setLayoutManager(layoutManager);
        seatRAdapter = new SeatRAdapter();
        //设置适配器所需要的信息
        ArrayList<ClassroomBean.Data> lassroomList = new ArrayList<>();
        seatRAdapter.setContext(SeatReservationActivity.this);
        seatRAdapter.setDay(day);
        seatRAdapter.setTime(time);
        seatRAdapter.setLassroomList(lassroomList);
        recyclerView.setAdapter(seatRAdapter);
    }


    //初始化数据  网络请求
    private void initListOne() {
        time = mTimeSpinner.getText().toString().substring(5);
        String date = TimeDate.getYear() + "/" + day.substring(0, 2) + "/" + day.substring(3) + " " + time;
        //Log.d("TAG",date);
        HashMap<String, Object> map = new HashMap<>();
        map.put("startTime", date);
        HttpUtil.httpPost(BuildConfig.SELECT_ROOM, map, HttpUtil.ContentType.FROM, new HttpUtil.CallBack() {
            @Override
            public void success(HttpUtil.Result result) {
                String content = result.content;
                if (content != null) {
                    try {
                        Gson gson = new Gson();
                        ClassroomBean classroomBean = gson.fromJson(content, ClassroomBean.class);
                        int code = classroomBean.getCode();
                        if (code == 200) {
                            ArrayList<ClassroomBean.Data> data = classroomBean.getData();
                            changeAdapter(data);
                        } else {
                            ToastUtil.ToastShort(SeatReservationActivity.this, "服务器链接失败，请检查网络链接");
                        }

                    } catch (JsonSyntaxException e) {
                        ToastUtil.ToastShort(SeatReservationActivity.this, "服务器链接失败，请检查网络链接");
                    }
                } else {
                    ToastUtil.ToastShort(SeatReservationActivity.this, "服务器链接失败，请检查网络链接");
                }

            }

            @Override
            public void failed(IOException e) {
                ToastUtil.ToastShort(SeatReservationActivity.this,"查询失败");
            }
        });
    }


    //上拉刷新
    @SuppressLint("ResourceAsColor")
    private void refresh() {
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                initListOne();
                swipeRefreshLayout.setRefreshing(false);
                ToastUtil.ToastShort(SeatReservationActivity.this,"刷新成功");
            }
        });
    }


    //时间选择器
    public void onOptionText() {
        DialogConfig.setDialogStyle(DialogStyle.Three);//设置弹窗类型为从中间开始
        OptionPicker picker = new OptionPicker(this);
        picker.setData("6:00", "7:00", "8:00", "9:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00", "18:00", "19:00", "20:00", "21:00");
        picker.setOnOptionPickedListener(SeatReservationActivity.this);
        picker.setTitle("开始时间");
        picker.getWheelLayout().setOnOptionSelectedListener(new OnOptionSelectedListener() {
            @Override
            public void onOptionSelected(int position, Object item) {
                //实时回调
            }
        });
        picker.getWheelView().setStyle(R.style.WheelDefault);
        picker.show();
    }

    //确定后数据 回调
    @Override
    public void onOptionPicked(int position, Object item) {
        mTimeSpinner.setText("开始时间\n" + item);
        initListOne();
    }


    //刷新适配器
    private void changeAdapter(ArrayList<ClassroomBean.Data> data) {
        seatRAdapter.setDay(day);
        seatRAdapter.setTime(time);
        //Log.d("TAG", time);
        //Log.d("TAG", TimeDate.getNowaDays());
        seatRAdapter.setLassroomList(data);
        //刷新适配器
        seatRAdapter.notifyDataSetChanged();
    }

}





